package helper;

import activities.StatisticsActivity2;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;

public class CustomScrollView extends HorizontalScrollView{

	/**
	 * Current Scroll Direction -1 means left 0 undefined 1 means right
	 */
	private int currScrollDirection = 0;
	private int currPosX = 0;
	private int currChild = 0;
	public static boolean FLING_HANDLED = false;

	public int getCurrChild() {
		return currChild;
	}
	
	public void increaseChildIndex() {
		if(++currChild >= StatisticsActivity2.nrOfCharts){
			currChild = StatisticsActivity2.nrOfCharts - 1;
		}
	}
	
	public void decreaseChildIndex() {
		if(--currChild < 0){
			currChild = 0;
		}
	}
	public void setCurrPosX(int currPosX) {
		this.currPosX = currPosX;
	}
	public int getCurrPosX() {
		return currPosX;
	}

	public int getCurrScrollDirection() {
		return currScrollDirection;
	}

	public CustomScrollView(Context context) {
		super(context);
		init();
	}

	@Override
	public void fling(int velocityX) {
		FLING_HANDLED = true;
		Log.i("SCROLL", ""+velocityX);
		if (velocityX > 0) {
			currScrollDirection = 1;
		} else if (velocityX < 0) {
			currScrollDirection = -1;
		} else {
			currScrollDirection = 0;
		}

		super.fling(velocityX);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		Log.i("SCROLL", "in dispatch von scrollview");	
		this.activity.dismissExerciseSelectionOverlay();
		FLING_HANDLED = false;
		this.activity.mChartView.callOnClick();
		this.activity.mChartView.dispatchTouchEvent(ev);
		
		return super.dispatchTouchEvent(ev);
	}
	
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {		
		super.onScrollChanged(l, t, oldl, oldt);
		this.currPosX = l;
	}

	public void init() {
		scrollerTask = new Runnable() {

			public void run() {

				int newPosition = getScrollY();
				if (initialPosition - newPosition == 0) {// has stopped

					if (onScrollStoppedListener != null) {
						onScrollStoppedListener.onScrollStopped();
					}
				} else {
					initialPosition = getScrollY();
					CustomScrollView.this.postDelayed(scrollerTask, newCheck);
				}
			}
		};
	}

	public CustomScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();

	}

	public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private Runnable scrollerTask;
	private int initialPosition;

	private int newCheck = 100;

	public interface OnScrollStoppedListener {
		void onScrollStopped();
	}

	private OnScrollStoppedListener onScrollStoppedListener;
	private StatisticsActivity2 activity;

	public void setOnScrollStoppedListener(
			CustomScrollView.OnScrollStoppedListener listener) {
		onScrollStoppedListener = listener;
	}

	public void startScrollerTask() {
		initialPosition = getScrollX();
		CustomScrollView.this.postDelayed(scrollerTask, newCheck);
	}

	public void setActivity(StatisticsActivity2 statisticsActivity2) {
		this.activity = statisticsActivity2;
		
	}
}